package com.my.mvvmrapid.ui.fuction.from;

import android.content.Context;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.my.mvvmrapid.BR;
import com.my.mvvmrapid.R;
import com.my.mvvmrapid.databinding.ActivityFromBinding;
import com.my.mvvmrapid.ui.main.fragment.homeshop.adapter.HomeShopFragmentStateAdapter;
import com.my.mvvmrapid.ui.main.fragment.homeshop.shoptest.HomeShopTestFragment;
import com.my.mvvmrapid.view.ScaleTransitionPagerTitleView;
import com.zhongjh.mvvmrapid.base.ui.BaseActivity;
import com.zhongjh.mvvmrapid.utils.ScreenUtil;

import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.List;

/**
 * 用于表单形式
 *
 * @author zhongjh
 * @date 2021/8/15
 */
public class FromActivity extends BaseActivity<ActivityFromBinding, FromViewModel> {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ScreenUtil.setFullScreen(FromActivity.this, false);
        super.onCreate(savedInstanceState);
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_from;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public boolean isStartAutoHideSoftKeyboard() {
        return false;
    }

    @Override
    public void initParam() {

    }

    @Override
    public void initData() {
        // 初始化toolbar
        binding.toolbar.inflateMenu(R.menu.menu_from_list);
        initToolbar(binding.toolbar, "我的工单", true);

        initMagicIndicator();
    }

    @Override
    public void initViewObservable() {

    }

    /**
     * 初始化顶部菜单
     */
    private void initMagicIndicator() {
        List<String> listTitle = new ArrayList<>();
        List<Fragment> listFragments = new ArrayList<>();

        listTitle.add("待接单");
        listFragments.add(HomeShopTestFragment.newInstance());
        listTitle.add("待处理");
        listFragments.add(HomeShopTestFragment.newInstance());
        listTitle.add("已完成");
        listFragments.add(HomeShopTestFragment.newInstance());

        CommonNavigator commonNavigator = new CommonNavigator(getApplication());
        commonNavigator.setAdjustMode(true);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return listTitle == null ? 0 : listTitle.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ScaleTransitionPagerTitleView(context);
                simplePagerTitleView.setText(listTitle.get(index));
                simplePagerTitleView.setTextSize(18);
                simplePagerTitleView.setNormalColor(getResources().getColor(R.color.grey_characters));
                simplePagerTitleView.setSelectedColor(getResources().getColor(R.color.brown));
                simplePagerTitleView.setOnClickListener(v -> binding.viewPager2.setCurrentItem(index));
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setColors(getResources().getColor(R.color.brown));
                return indicator;
            }

            @Override
            public float getTitleWeight(Context context, int index) {
                return 1.0f;
            }
        });
        binding.magicIndicator.setNavigator(commonNavigator);

        binding.viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
                binding.magicIndicator.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                binding.magicIndicator.onPageSelected(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
                binding.magicIndicator.onPageScrollStateChanged(state);
            }
        });

        HomeShopFragmentStateAdapter homeFragmentPageAdapter = new HomeShopFragmentStateAdapter(getSupportFragmentManager(), getLifecycle());
        binding.viewPager2.setAdapter(homeFragmentPageAdapter);
        binding.viewPager2.setOffscreenPageLimit(listFragments.size());
    }

}
